package com.yswy.backstage.service.impl;

import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.backstage.dto.req.TgChannleListVo;
import com.yswy.backstage.entity.user.User;
import com.yswy.backstage.service.PackageManageService;
import com.yswy.backstage.service.TgChannleChildCodeService;
import com.yswy.backstage.service.TgChannleChildService;
import com.yswy.common.utils.DateUtils;
import com.yswy.common.utils.HttpUtil;
import com.yswy.common.utils.StringUtils;
import com.yswy.domain.entity.PackageManage;
import com.yswy.domain.entity.TgChannleChild;
import com.yswy.domain.entity.TgChannleChildCode;
import com.yswy.domain.mapper.PackageManageMapper;
import com.yswy.domain.mapper.TgChannleChildCodeMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class TgChannleChildCodeServiceImpl extends ServiceImpl<TgChannleChildCodeMapper, TgChannleChildCode>
        implements TgChannleChildCodeService {
    @Value("${zjym}")
    private String zjym;
    @Autowired
    private TgChannleChildService tgChannleChildService;
    @Override
    public Page<TgChannleChildCode> listTgChannleChildCode(TgChannleListVo tgChannleListVo) {
        Page<TgChannleChildCode> page = page(new Page<>(tgChannleListVo.getPageNum(), tgChannleListVo.getPageSize()),
                new LambdaQueryWrapper<TgChannleChildCode>()
                        .eq(TgChannleChildCode::getTgChannleChildId , tgChannleListVo.getTgChannleChildId())
                        .ge(tgChannleListVo.getStaTime()!= null ,TgChannleChildCode::getCreateTime , tgChannleListVo.getStaTime())
                        .le(tgChannleListVo.getEndTime()!= null ,TgChannleChildCode::getCreateTime , tgChannleListVo.getEndTime())
        );
        return page;
    }

    @Override
    public void addOrUpdateTgChannleChildCode(TgChannleChildCode tgChannleChildCode, User user) {



        if(tgChannleChildCode.getCodId() != null){
            tgChannleChildCode.setCreateBy(user.getId().longValue());
            tgChannleChildCode.setCreateTime(new Date());
            TgChannleChild tgChannleChild = tgChannleChildService.getById(tgChannleChildCode.getTgChannleChildId());

            HttpUtil.doGet(zjym+"tg/delDeduction?channle="+tgChannleChild.getChannle()
                    +"&star="+tgChannleChildCode.getStartTime()
                    +"&stop="+tgChannleChildCode.getEndTime());

        }else {
            List<TgChannleChildCode> tgChannleChildCodes =list(new LambdaQueryWrapper<TgChannleChildCode>()
                    .eq(TgChannleChildCode::getTgChannleChildId ,tgChannleChildCode.getTgChannleChildId() ));

            if (tgChannleChildCodes.size()>0){
                for (TgChannleChildCode t:
                        tgChannleChildCodes) {
                    if (DateUtils.belongCalendar(tgChannleChildCode.getStartTime() ,tgChannleChildCode.getEndTime() , t.getEndTime() , "HH:mm:ss" ) ||
                            DateUtils.belongCalendar(tgChannleChildCode.getStartTime() ,tgChannleChildCode.getEndTime() , t.getStartTime() , "HH:mm:ss" ) ||
                            DateUtils.belongCalendar(t.getStartTime() ,t.getEndTime() , tgChannleChildCode.getStartTime() , "HH:mm:ss" ) ||
                            DateUtils.belongCalendar(t.getStartTime() ,t.getEndTime() , tgChannleChildCode.getStartTime() , "HH:mm:ss" )){
                        throw new IllegalStateException("配置时间冲突");
                    }
                }
            }
            tgChannleChildCode.setUpdateBy(user.getId().longValue());
            tgChannleChildCode.setUpdateTime(new Date());
        }
        saveOrUpdate(tgChannleChildCode);
    }

    @Override
    public void delTgChannleChildCode(Long Id, User user) {
        removeById(Id);
    }
}
